﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ADECMS
{
    public partial class Registration : System.Web.UI.Page
    {
        ADECMSDBDataContext cmsdb;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                cmsdb = new ADECMSDBDataContext();
            }
            catch (Exception ex)
            {
            }
            LabelIme.Text = "";
            LabelPrezime.Text = "";
            LabelUserName.Text = "";
            LabelPass.Text = "";
            LabelEmail.Text = "";
            LabelAvatarUpload.Text = "";

        }

        protected bool Provjeri_Korisnicko_ime()
        {
            if (UserName.Text.Length == 0)
            {
                LabelUserName.Text = "Morate unijeti korisničko ime";
                return false;
            }
            else
            {
                var useri = from k in  cmsdb.USERs
                            where k.username == UserName.Text
                            select k;

                try
                {
                    USER kor = useri.First();
                    LabelUserName.Text = "Traženo korisničko ime već postoji, izaberite neko drugo";
                    return false;

                }
                catch (System.InvalidOperationException)
                {
                    LabelUserName.Text = "Traženo korisničko ime je dostupno";
                    return true;
                }
            }
        }

        protected void Dostupnost_Click(object sender, EventArgs e)
        {
            Provjeri_Korisnicko_ime();
        }



        protected void Submit_Click(object sender, EventArgs e)
        {
            if (Ime.Text.Length == 0) { LabelIme.Text = "Morate unijeti ime"; return; }
            if (Prezime.Text.Length == 0) { LabelPrezime.Text = "Morate unijeti prezime"; return; }
            if (!Provjeri_Korisnicko_ime()) return;
            if (Pass.Text.Length == 0) { LabelPass.Text = "Morate unijeti šifru"; return; }
            if (PassConf.Text.Length == 0) { LabelPass.Text = "Morate potvrditi šifru"; return; }
            if (Pass.Text != PassConf.Text) { LabelPass.Text = "Šifre se ne podudaraju"; return; }
            if (email1.Text.Length == 0 || email2.Text.Length == 0 || email3.Text.Length == 0)
            { LabelEmail.Text = "Morate unijeti email"; return; }

            String email = email1.Text + "@" + email2.Text + "." + email3.Text;
            USER novikor = new USER();
            USER_OPTION dopcije = new USER_OPTION();
            if (AvatarUpload.HasFile)
            {
                if (AvatarUpload.PostedFile.ContentType.ToString() != "image/jpeg")
                {
                    LabelAvatarUpload.Text = "File koji ste željeli postaviti za avatar nije jpeg slika";
                    return;
                }
                if (AvatarUpload.PostedFile.ContentLength > 5000000)
                {
                    LabelAvatarUpload.Text = "Veličina slike je " + AvatarUpload.PostedFile.ContentLength.ToString();
                    return;
                }

                dopcije.avatar = AvatarUpload.FileBytes;
            }
            dopcije.description = Opis.Text;

            novikor.USER_OPTION = dopcije;
            novikor.name = Ime.Text;
            novikor.surname = Prezime.Text;
            novikor.username = UserName.Text;
            novikor.password = Pass.Text;
            novikor.email = email;
            novikor.registration_date = DateTime.Now;
            novikor.ban = false;
            try
            {
                USER_TYPE priv = cmsdb.USER_TYPEs.Single(p => p.type == "Korisnik");
                priv.USERs.Add(novikor);
                cmsdb.USERs.InsertOnSubmit(novikor);
                cmsdb.USER_OPTIONs.InsertOnSubmit(dopcije);
                cmsdb.SubmitChanges();
            }
            catch (NullReferenceException)
            {
                return;
            }

            Response.Redirect("Default.aspx"); 
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }
    }
}
